$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/order/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '订单编码', name: 'orderCode', index: 'order_code', width: 90},
            // { label: '商品id', name: 'commodityId', index: 'commodity_id', width: 80 },
            // { label: '用户id', name: 'userId', index: 'user_id', width: 80 },
            // { label: '商品主人即设计师等id', name: 'commodityMasterId', index: 'commodity_master_id', width: 80 },
            // { label: '商品主人所在店铺id', name: 'commodityMasterStoreId', index: 'commodity_master_store_id', width: 80 },
            {label: '店铺名称', name: 'storeName', index: 'store_name', width: 80},
            // { label: '扫码时间 店铺扫码用户', name: 'scanCodeTime', index: 'scan_code_time', width: 80 },
            // { label: '收益 平台抽佣之后店铺所获得的金额', name: 'reapMoney', index: 'reap_money', width: 80 },
            // { label: '商品图片路径', name: 'imgurl', index: 'imgUrl', width: 80 },
            {label: '商品名称', name: 'commodityName', index: 'commodity_Name', width: 40},
            {label: '商品数量', name: 'commodityNumber', index: 'commodity_number', width: 40},
            {label: '联系人', name: 'contact', index: 'contact', width: 40},
            {label: '手机号', name: 'phone', index: 'phone', width: 50},
            {
                label: '订单状态',
                name: 'orderStatus',
                index: 'order_status',
                width: 40,
                formatter: function (value, options, row) {
                    if (value === 0) {
                        return '<span class="label label-primary">待付款</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-success">待接单</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-primary">已接单</span>';
                    }
                    if (value === 3) {
                        return '<span class="label label-success">已拒单</span>';
                    }
                    if (value === 4) {
                        return '<span class="label label-success">待使用</span>';
                    }
                    if (value === 5) {
                        return '<span class="label label-primary">已完成</span>';
                    }
                    if (value === 6) {
                        return '<span class="label label-success">待评价</span>';
                    }
                    if (value === 7) {
                        return '<span class="label label-success">售后</span>';
                    }
                }
            },
            {label: '商品地址', name: 'orderAddress', index: 'order_address', width: 140},
            {label: '订单实付金额', name: 'orderPrice', index: 'order_price', width: 40},
            // { label: '订单支付类型', name: 'orderpayType', index: 'orderPay_type', width: 80 },
            // { label: '订单接受状态 0新订单 1已接单 2已拒单 3已完成', name: 'orderAcceptStatus', index: 'order_accept_status', width: 80 },
            {label: '预约时间', name: 'appointmentTime', index: 'appointment_time', width: 80},
            // { label: '创建时间/付款时间', name: 'createTime', index: 'create_time', width: 80 },
            // {label: '订单显示状态', name: 'showStatus', index: 'show_status', width: 80,
            //     formatter: function (value, options, row) {
            //         if (value === 0) {
            //             return '<span class="label label-primary">已删除</span>';
            //         }
            //         if (value === 1) {
            //             return '<span class="label label-success">未删除</span>';
            //         }
            //     }},
            // {label: '买家留言', name: 'buyerMassage', index: 'buyer_massage', width: 80},
            // { label: '创建的月份', name: 'createMonth', index: 'create_month', width: 80 },
            // { label: '创建的年份', name: 'createYears', index: 'create_years', width: 80 },
            // { label: '接单和拒单时间', name: 'acceptandrefuseOrderTime', index: 'acceptAndRefuse_order_time', width: 80 },
            // { label: '总订单编号', name: 'orderheaderCode', index: 'orderHeader_code', width: 80 },
            // { label: '拒单原因', name: 'whyrefuseorder', index: 'whyRefuseOrder', width: 80 },
            // { label: '//订单金额 有抵扣的存在', name: 'price', index: 'price', width: 80 },
            // { label: '订单向店铺支付密码', name: 'paytostorepassword', index: 'payToStorePassword', width: 80 },
            // { label: '状态更新时间', name: 'updateTime', index: 'update_time', width: 80 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        q:{
            orderCode : null,
            contact : null
        },
        order: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.order = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.order.id == null ? "sys/order/save" : "sys/order/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.order),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/order/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/order/info/" + id, function (r) {
                vm.order = r.order;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData:{
                    'orderCode': vm.q.orderCode,
                    'contact': vm.q.contact
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});